package com.example.myfr;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.myfr.adatper.Card1listAdapter;
import com.example.myfr.model.Listitem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Card1Activity extends Activity {
    String JSON_URL="https://raw.githubusercontent.com/Lindaszpt/education/master/azure_anime.json";
//    String JSON_URL="https://121.40.121.200/manhua.json";
    private JsonArrayRequest request;
    RecyclerView mRecyclerView;
    Card1listAdapter card1listAdapter;
    private RequestQueue requestQueue;
    List<Listitem> mListIte;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card1);

        mRecyclerView=findViewById(R.id.recyclerviemid);
        mListIte =new ArrayList<>();
        getJsonDate();

    }
    private void getJsonDate(){

        request= new JsonArrayRequest(JSON_URL,new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                JSONObject jsonObject=null;
                for (int i=0;i<response.length();i++){
                    Listitem listitem=new Listitem();
                    try {
                        jsonObject=response.getJSONObject(i);
                        listitem.setName(jsonObject.getString("name"));
                        listitem.setDescription(jsonObject.getString("description"));
                        listitem.setCaetgoire(jsonObject.getString("categorie"));
                        listitem.setRating(jsonObject.getString("Rating"));
                        listitem.setNb_episode(jsonObject.getInt("episode"));
                        listitem.setImg_url(jsonObject.getString("img"));
                        listitem.setStudio(jsonObject.getString("studio"));
                        mListIte.add(listitem);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Log.d("jinx", "onResponse: "+mListIte.size());
                card1listAdapter = new Card1listAdapter(getApplicationContext(),mListIte);
                mRecyclerView.setAdapter(card1listAdapter);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue= Volley.newRequestQueue(Card1Activity.this);
        requestQueue.add(request);
    }
//    private void initRecyclerViem(List<Listitem> list){
//
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//
//    }


}
